from mysql.connector import Error
import threading

import mysql.connector

class MySQLConnection:
    _instance = None
    _lock = threading.Lock()

    def __new__(cls, *args, **kwargs):
        if not cls._instance:
            with cls._lock:
                if not cls._instance:
                    cls._instance = super(MySQLConnection, cls).__new__(cls)
        return cls._instance

    def __init__(self, host, database, user, password):
        if not hasattr(self, 'connection'):
            try:
                self.connection = mysql.connector.connect(
                    host=host,
                    database=database,
                    user=user,
                    password=password
                )
                if self.connection.is_connected():
                    print("Connected to MySQL database")
            except Error as e:
                print(f"Error while connecting to MySQL: {e}")

    def get_connection(self):
        return self.connection
